%%
close all;
clear;
clc;
%% prepare data
step = 1;

template = [1 1 1 1 1 1 1 1 1 1 1 1 1 1;
            1 1 1 1 1 2 2 2 2 1 1 1 1 1;
            1 1 1 1 2 2 2 2 2 2 1 1 1 1;
            1 1 1 2 2 2 1 1 2 2 2 1 1 1;
            1 1 2 2 2 1 1 1 1 2 2 2 1 1;
            1 2 2 2 1 1 1 1 1 1 2 2 2 1;
            1 2 2 1 1 1 1 1 1 1 1 2 2 1;
            1 2 2 1 1 1 1 1 1 1 1 2 2 1;
            1 2 2 2 1 1 1 1 1 1 2 2 2 1;
            1 1 2 2 2 1 1 1 1 2 2 2 1 1;
            1 1 1 2 2 2 1 1 2 2 2 1 1 1;
            1 1 1 1 2 2 2 2 2 2 1 1 1 1;
            1 1 1 1 1 2 2 2 2 1 1 1 1 1;
            1 1 1 1 1 1 1 1 1 1 1 1 1 1];

filling = ones(size(template));
signal = [  filling  2-template   filling;
           template     filling  template;
            filling  2-template   filling];

%% correlation matching
correlation2D = crossCorrelation2D(template, signal);
correlation2DExtremums = extremums(correlation2D);
%% plot correlation results
figure('Color','white');
mesh(correlation2D);
hold on;
mesh(correlation2DExtremums + 2);
hold on;
mesh(signal + 3);
%% SAD matching
sadEfficiency = sad2D(signal, template);
sadEfficiencyExtremums = extremums(sadEfficiency);
%% plot SAD results
figure('Color','white');
mesh(sadEfficiency);
hold on;
mesh(sadEfficiencyExtremums + 2);
hold on;
mesh(signal + 3);
%% perform KP matching
kpEfficiency = kunchenko2D(template, signal, step);
kpEfficiencyExtremums = extremums(kpEfficiency);
%% plot KP results
figure('Color','white');
mesh(kpEfficiency);
hold on;
mesh(kpEfficiencyExtremums + 2);
hold on;
mesh(signal + 3);
